package no21.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EvenChecker implements Runnable{

	private IntGenerator generator;//共享内在中
	private final int id;
	
	public EvenChecker(IntGenerator g,int ident){
		generator = g;
		id =ident;
	}
	@Override
	public void run() {
		while(!generator.iscanceled()){
			int val = generator.next();
		 //val = generator.next1();
			//不为偶数时
			if(val % 2 != 0){
				System.out.println(val + " not even!");
				generator.cancel();
			}
		}
	}
	
	public static void test(IntGenerator gp,int count){
		System.out.println("Press Control -C to exit");
		ExecutorService exec = Executors.newCachedThreadPool();
		for (int i = 0; i < count; i++) {
			exec.execute(new EvenChecker(gp,i));
		}
		exec.shutdown();
	}

	public static void test(IntGenerator gp){
		test(gp,1000);
	}
}
